<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */

namespace EasySwoole\EasySwoole;


use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use App\HttpController\Index;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
//        var_dump($request);
//        var_dump($request->getUri()->getPath());  //打印访问的路径

        $reqPath = $request->getUri()->getPath();
        $reqPath = rtrim($reqPath,"/");
//        var_dump($reqPath); //如果本来只有 '/'  ，那么就是 ''  , string

        switch($reqPath){
            case "/fuck":
                $obj = new Index($request);
                $msg = $obj->fuck($request);
                break;

            default:
                $obj = new Index($request);
                $msg = $obj->index();
                break;
        }//end

        $msg = json_encode($msg,JSON_UNESCAPED_UNICODE);
        $response->withHeader('Content-type','application/json;charset=utf-8');
        $response->write($msg);
        $response->end();
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }

    public static function onReceive(\swoole_server $server, int $fd, int $reactor_id, string $data):void
    {

    }

}